/*
 * JoinConferenceServlet.java
 *
 * Created on April 18, 2007, 12:26 AM
 */

package clicktodial.web;

import clicktodial.model.ModelFacade;
import clicktodial.model.Person;
import java.io.IOException;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.sip.Address;
import javax.servlet.sip.SipApplicationSession;
import javax.servlet.sip.SipFactory;
import javax.servlet.sip.SipServletRequest;

/**
 *
 * @author jkaplan
 * @version
 */
public class PlaceCallServlet extends HttpServlet {
    private static final Logger logger =
            Logger.getLogger(PlaceCallServlet.class.getName());
    
    // inject the SipFactory    
    @Resource
    private SipFactory sf;
    
    /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     */
    protected void processRequest(HttpServletRequest request, 
                                  HttpServletResponse response)
        throws ServletException, IOException 
    {
        String sourceUserName = request.getParameter("source");
        String targetUserName = request.getParameter("target");
        
        ModelFacade mf = (ModelFacade) getServletContext().getAttribute("Model");
        Person source = mf.getPerson(sourceUserName);
        Person target = mf.getPerson(targetUserName);

        String sourcePhone = source.getTelephone();
        String targetPhone = target.getTelephone();
      
        SipApplicationSession appSession = sf.createApplicationSession();
        Address to = sf.createAddress(sourcePhone);
        Address from = sf.createAddress(targetPhone);
        
        logger.info("Sending invite from " + from + " to " + to);
        
        // create an INVITE request to the first party from the second
        SipServletRequest req = sf.createRequest(appSession, "INVITE", from, to);
        
        // store the second party's address in the sip session
        req.getSession().setAttribute("SecondPartyAddress", from);
        req.getSession().setHandler("CallSipServlet");
        
        // create an SDP            
        req.send();
    }   
 
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
    
    /** Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
    
    /** Returns a short description of the servlet.
     */
    public String getServletInfo() {
        return "Short description";
    }
    // </editor-fold>
}
